package com.cardbaobao.cardbabyclient.utils;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

/**
 * 动画工具类.
 */
public class AnimationUtil {

    private static RotateAnimation rotateAnimation;
    /**
     * 旋转动画
     * @param view 要旋转的View
     */
    public static void rotateView(View view){
        if(rotateAnimation == null){
            synchronized (RotateAnimation.class){
                if(rotateAnimation == null){
                    rotateAnimation = new RotateAnimation(0f,360f, Animation.RELATIVE_TO_SELF,
                            0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                    rotateAnimation.setDuration(1000);//设置动画持续时间
                    rotateAnimation.setRepeatCount(-1);//不停旋转
                    rotateAnimation.setFillAfter(false);
                    rotateAnimation.setInterpolator(new LinearInterpolator());
                }
            }
        }
        view.startAnimation(rotateAnimation);
    }

    /**
     * 停止旋转动画
     */
    public static void stopRotateAnim(){
        if(rotateAnimation != null){
            rotateAnimation.setRepeatCount(1);//继续旋转1次之后再停止
            rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    rotateAnimation.cancel();
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }
    }


    /**
     * 将view从不可见变为可见的动画，原理:动态改变其LayoutParams.height的值
     * @param view 要展示动画的view
     */
    public static void visibleAnimator(final View view){
        if(view!=null) {
            int viewHeight=view.getHeight();
            if(viewHeight==0){
                int width=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
                int height=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
                view.measure(width,height);
                viewHeight=view.getMeasuredHeight();
            }
            ValueAnimator animator=ValueAnimator.ofInt(0,viewHeight);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewGroup.LayoutParams params=view.getLayoutParams();
                    params.height= (int) animation.getAnimatedValue();
                    view.setLayoutParams(params);
                }
            });
            animator.start();
        }
    }

    /**
     * 将view从可见变为不可见的动画，原理:动态改变其LayoutParams.height的值
     * @param view 要展示动画的view
     */
    public static void invisibleAnimator(final View view){
        if(view!=null){
            int viewHeight=view.getHeight();
            if(viewHeight==0){
                int width=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
                int height=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
                view.measure(width,height);
                viewHeight=view.getMeasuredHeight();
            }
            ValueAnimator animator=ValueAnimator.ofInt(viewHeight,0);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewGroup.LayoutParams params=view.getLayoutParams();
                    params.height= (int) animation.getAnimatedValue();
                    view.setLayoutParams(params);
                }
            });
            animator.start();
        }
    }

    /**
     * 动态改变view的高度动画效果，动画时长300毫秒[android属性动画默认时长]
     * 原理:动画改变view LayoutParams.height的值
     * @param view 要进行高度改变动画的view
     * @param startHeight 动画前的view的高度
     * @param endHeight 动画后的view的高度
     */
    public static void changeViewHeightAnimatorStart(final View view,final int startHeight,final int endHeight){
        if(view!=null&&startHeight>=0&&endHeight>=0){
            if(view.getLayoutParams().height!=endHeight){
                ValueAnimator animator=ValueAnimator.ofInt(startHeight,endHeight);
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        ViewGroup.LayoutParams params=view.getLayoutParams();
                        params.height= (int) animation.getAnimatedValue();
                        view.setLayoutParams(params);
                    }
                });
                animator.start();
            }
        }
    }
}
